<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-item label="采购合同编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['purchaseNum']" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="采购合同名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['purchaseName']" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="采购合同类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['purchaseType']" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="采购合同金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['purchaseMoney']" disabled><span slot="addonAfter">元</span></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="计划付款金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['expectMoney']" disabled><span slot="addonAfter">元</span></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="实际付款金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['actualMoney']" disabled><span slot="addonAfter">元</span></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="付款条件" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['payCondition']" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="开户行" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['bankDeposit', validatorRules.bankDeposit]"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="公司名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['billCompany', validatorRules.billCompany]"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="账号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['taxNum', validatorRules.taxNum]"></a-input>
            </a-form-item>
          </a-col>
          <!--          <a-col :span="12">-->
          <!--            <a-form-item label="付款条件" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
          <!--              <j-popup-->
          <!--                v-decorator="['payCondition']"-->
          <!--                :trigger-change="true"-->
          <!--                org-fields="id,pay_condition"-->
          <!--                dest-fields="paymentId,payCondition"-->
          <!--                code="contract_purchase_payment_select"-->
          <!--                @callback="popupCallback"-->
          <!--              />-->
          <!--              <a-input v-decorator="['paymentId']" v-show='false'></a-input>-->
          <!--            </a-form-item>-->
          <!--          </a-col>-->

          <!-- <a-col :span="12">
            <a-form-item label="创建人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['createBy']" disabled></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="创建时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="" disabled v-decorator="['createTime']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-item label="是否收票" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-switch v-decorator="['billing', validatorRules.billing]" @change="billChange"></j-switch>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="收票日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                placeholder="请选择收票日期"
                v-decorator="['receiveBilllingTime', validatorRules.receiveBilllingTime]"
                :trigger-change="true"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="发票金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                placeholder="请输入发票金额"
                v-decorator="['invoicedAmount', validatorRules.invoicedAmount]"
                type="number"
                style="width: 100%"
              >
                <span slot="addonAfter">元</span>
              </a-input>
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-upload v-decorator="['accessories', validatorRules.accessories]" :trigger-change="true"></j-upload>
            </a-form-item>
          </a-col>

          <a-col :span="24">
            <a-form-item label="特殊说明" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <a-textarea v-decorator="['remarks', validatorRules.remarks]" rows="4" placeholder="请输入特殊说明" />
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm" type="primary">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import { filterDictTextByCache } from '@/components/dict/JDictSelectUtil'

export default {
  name: 'ContractPurchaseApplyPaymentForm',
  components: {},
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => {},
      required: false,
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false,
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 3 },
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 20 },
      },
      confirmLoading: false,
      validatorRules: {
        remarks: {
          rules: [{ required: false, message: '请输入特殊说明!' }],
        },
        billing: {
          initialValue: 'Y',
        },

        bankDeposit: {
          rules: [{ required: true, message: '请输入开户行!' }],
        },
        billCompany: {
          rules: [{ required: true, message: '请输入公司名称!' }],
        },
        taxNum: {
          rules: [{ required: true, message: '请输入账号!' }],
        },
        receiveBilllingTime: {
          rules: [{ required: true, message: '请选择收票日期!' }],
        },
        invoicedAmount: {
          rules: [{ required: true, message: '请输入发票金额!' }],
        },
        accessories: {
          rules: [{ required: true, message: '请选择附件!' }],
        },
        payMoney: {
          rules: [
            { required: false },
            { pattern: /^(([0-9]*)|([0]\.\d{0,4}|[0-9]*\.\d{0,4}))$/, message: '请输入正确的金额!' },
          ],
        },
      },
      url: {
        add: '/contract/contractPurchaseApplyPayment/add',
        edit: '/contract/contractPurchaseApplyPayment/edit',
        queryById: '/contract/contractPurchaseApplyPayment/queryById',
      },
      fields: [
        'applicant',
        'applyTime',
        'receiveBilllingTime',
        'payMoney',
        'accessories',
        'paymentId',
        'bpmStatus',
        'remarks',
        'purchaseType',
        'purchaseMoney',
        'createBy',
        'createTime',
        'payCondition',
        'purchaseName',
        'purchaseNum',
        'supplierName',
        'contractSaleName',
        'contractSaleNum',
        'customerName',
        'opportunityName',
        'opportunityNum',
        'actualMoney',
        'expectMoney',
        'invoicedAmount',
        'bankDeposit',
        'billCompany',
        'taxNum',
        'billing',
      ],
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    },
  },
  created() {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData()
  },
  methods: {
    bill() {
      return false
    },
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      record.purchaseType = filterDictTextByCache('icm_contract_purchase_type', record.purchaseType)
      if (!record.billing) {
        record.billing = 'Y'
        this.billChange(record.billing)
      }
      this.model = Object.assign({}, record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, this.fields))
      })
    },
    //渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = { id: this.formData.dataId }
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result)
          }
        })
      }
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          console.log(this.model)
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          console.log('表单提交数据', formData)
          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    billChange(value) {
      if (value == 'Y') {
        this.validatorRules.receiveBilllingTime.rules[0].required = true
        this.validatorRules.invoicedAmount.rules[0].required = true
        this.validatorRules.accessories.rules[0].required = true
        this.validatorRules.remarks.rules[0].required = false
      } else {
        this.validatorRules.receiveBilllingTime.rules[0].required = false
        this.validatorRules.invoicedAmount.rules[0].required = false
        this.validatorRules.accessories.rules[0].required = false
        this.validatorRules.remarks.rules[0].required = true
      }
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, this.fields))
    },
  },
}
</script>